package com.lebang.service.cences;

import com.lebang.configuration.datasource.DataSource;
import com.lebang.domain.CencesClickInfo;
import com.lebang.domain.MemberClickRouse;
import com.lebang.domain.TaskRouse;
import com.lebang.entity.RequestHeaders;
import com.lebang.example.CencesClickInfoExample;
import com.lebang.mapper.CencesClickInfoMapper;
import com.lebang.model.MyConfig;
import com.lebang.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 签到应用详情
 *
 * @Author hwq
 * @Date 2020-04-16
 */
@Service
public class CencesClickInfoService extends BaseService<CencesClickInfo, Integer, CencesClickInfoExample> {
    @Autowired
    private CencesClickInfoMapper baseMapper;

    /**
     * 统计签到
     * @param headers
     * @param rouses
     * @param star
     * @param taskRouse
     */
    public void addClick(RequestHeaders headers, MemberClickRouse rouses, int star, TaskRouse taskRouse) {
        Integer id = baseMapper.checkExits(headers.getAppStore(), taskRouse.getSdkKey(), rouses.getAppName(), MyConfig.getEnv());
        if (id != null) {
            baseMapper.addCountById(id, star);
        } else {
            CencesClickInfo data = new CencesClickInfo(1,MyConfig.getEnv());
            data.setAppStore(headers.getAppStore());
            data.setSdkKey(taskRouse.getSdkKey());
            data.setSdkName(taskRouse.getTaskName());
            data.setPkgName(rouses.getPkgName());
            data.setAppName(rouses.getAppName());
            data.setIcon(rouses.getIcon());
            baseMapper.insertClickInfo(data);
        }
    }

    /**
     * @param appStore
     * @param pkgName
     * @param appName
     * @param star
     * @param sdkKey
     * @param sdkName
     * @param icon
     */
    public void addClick(String appStore,
                         String pkgName,
                         String appName,
                         int star,
                         String sdkKey,String sdkName,String icon) {
        Integer id = baseMapper.checkExits(appStore, sdkKey, appName,MyConfig.getEnv());
        if (id != null) {
            baseMapper.addCountById(id, star);
        } else {
            CencesClickInfo data = new CencesClickInfo(1,MyConfig.getEnv());
            data.setAppStore(appStore);
            data.setSdkKey(sdkKey);
            data.setSdkName(sdkName);
            data.setPkgName(pkgName);
            data.setAppName(appName);
            data.setIcon(icon);
            baseMapper.insertClickInfo(data);
        }
    }
    public List<CencesClickInfo> page(CencesClickInfoExample example) {
    return baseMapper.page(example);
    }

    @DataSource("commonDB")
    public List<CencesClickInfo> selectclickInfo(CencesClickInfoExample example) {
        return  baseMapper.selectByExample(example);
    }

    @DataSource("commonDB")
    public long countClickInfo(CencesClickInfoExample example) {
        return baseMapper.countByExample(example);
    }
}
